﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Library.Connection;
using Gizmox.WebGUI.Forms;
using Library.SQL;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;

namespace Library
{
    public class SettingService
    {
        public SettingService()
        { }
        public static void ListViewSettingColumnWidthSave(ListView lv, string formname)
        {
            DataBaseConnectivity m_dataBaseConectivity = new DataBaseConnectivity();
            string columnwidth = "";
            foreach (ColumnHeader hdr in lv.Columns)
            {
                columnwidth += (hdr.Width.ToString() + ",");
            }
            try
            {
                m_dataBaseConectivity.MySqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SQLScript.DeleteSettings(), m_dataBaseConectivity.MySqlConnection);
                cmd.Parameters.Add("SettingName", MySqlDbType.VarChar).Value = lv.Name + formname;
                cmd.ExecuteNonQuery();
                cmd.CommandText = SQLScript.InsertSettings();
                cmd.Parameters.Add("setting1", MySqlDbType.VarChar).Value = columnwidth;
                cmd.Parameters.Add("setting2", MySqlDbType.VarChar).Value = "";
                cmd.Parameters.Add("setting3", MySqlDbType.VarChar).Value = "";
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.MySqlConnection.Close();
            }
        }
        public static void ListViewSettingColumnWidthLoad(ListView lv, string formname)
        {
            DataBaseConnectivity m_dataBaseConectivity = new DataBaseConnectivity();
            string columnwidth = "";
            try
            {
                m_dataBaseConectivity.MySqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SQLScript.SelectSettings(), m_dataBaseConectivity.MySqlConnection);
                cmd.Parameters.Add("SettingName", MySqlDbType.VarChar).Value = lv.Name + formname;
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                    columnwidth = rd["setting1"].ToString();
                rd.Close();
                if (columnwidth.Trim() != "")
                {
                    string[] width = columnwidth.Split(',');
                    foreach (ColumnHeader hdr in lv.Columns)
                    {
                        try
                        {
                            hdr.Width = Convert.ToInt32(width[hdr.Index]);
                        }
                        catch { }
                    }
                }
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.MySqlConnection.Close();
            }
        }
    }
}